package edu.gatech.volt2.drdoommgt.tests;

import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;

import edu.gatech.volt2.drdoommgt.domain.Doctor;
import edu.gatech.volt2.drdoommgt.domain.Name;
import edu.gatech.volt2.drdoommgt.domain.Nurse;
import edu.gatech.volt2.drdoommgt.domain.Patient;
import edu.gatech.volt2.drdoommgt.system.DataBaseAccessor;


public class DatabaseAccessor {
	
	@javax.persistence.PersistenceContext private static EntityManagerFactory emf = null;
	private static EntityManager em = null;
	
	@BeforeClass public static void setupClass(){
	}
	
	@Test public void connectToDatabase(){
		DataBaseAccessor db = new DataBaseAccessor("testing");
		// Connect
		db.connectToDatabase();
		assertTrue(db.isConnected());
		// Disconnect
		db.closeConnectionToDatabase();
		assertFalse(db.isConnected());
	}
	
	@Test public void createUserPatient(){
		// Connect
		DataBaseAccessor db = new DataBaseAccessor("testing");
		db.connectToDatabase();
		
		Patient patient = new Patient();
		patient.setEmail("testing_patient@drdoom.com");
		patient.setName(new Name("Testing", "User"));
		patient.setSuspended(false);
		patient.setPassword("Testing");
		assertTrue(db.createUser(patient));
		// Close Connection
		db.closeConnectionToDatabase();
	}
	
	@Test public void findUserPatientByEmail(){
		// Connect
		DataBaseAccessor db = new DataBaseAccessor("testing");
		db.connectToDatabase();
		// Find the patient by email
		Patient patient = db.findPatientByEmail("testing_patient@drdoom.com");
		assertEquals("testing_patient@drdoom.com",patient.getEmail());
		// Close Connection
		db.closeConnectionToDatabase();
	}
	
	@Test public void findUserPatientByName(){
		// Connect
		DataBaseAccessor db = new DataBaseAccessor("testing");
		db.connectToDatabase();
		// Find the patient by name
		Patient patient = db.findPatientByName(new Name("Testing", "User"));
		assertEquals(new Name("Testing", "User").getFirst(), patient.getName().getFirst());
		assertEquals(new Name("Testing", "User").getLast(), patient.getName().getLast());
		// Close Connection
		db.closeConnectionToDatabase();	
	}
	
	
	@Test public void removeUserPatient(){
		// Connect
		DataBaseAccessor db = new DataBaseAccessor("testing");
		db.connectToDatabase();
		// Find the patient by name
		Patient patient = db.findPatientByName(new Name("Testing", "User"));
		assertEquals(new Name("Testing", "User").getFirst(), patient.getName().getFirst());
		assertEquals(new Name("Testing", "User").getLast(), patient.getName().getLast());
		
		// Delete the patient 
		db.removeUser(patient);
		
		// Try finding them again
		patient = db.findPatientByName(new Name("Testing", "User"));
		assertNull(patient);
		
		// Close Connection
		db.closeConnectionToDatabase();	
	}

	

}
